#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

###############################################################################
# This script is used to export an image to qcow2 file
###############################################################################

# ------------ Set up the environment to source common tools ------------

if [ -z "${ONE_LOCATION}" ]; then
    LIB_LOCATION=/usr/lib/one
else
    LIB_LOCATION=$ONE_LOCATION/lib
fi

. $LIB_LOCATION/sh/scripts_common.sh

DRIVER_PATH=$(dirname $0)
source ${DRIVER_PATH}/../libfs.sh
source ${DRIVER_PATH}/ceph.conf

# -------- Get rm and datastore arguments from OpenNebula core ------------

DRV_ACTION=$1
ID=$2

XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION"

unset i XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH     /DS_DRIVER_ACTION_DATA/IMAGE/SOURCE \
                    /DS_DRIVER_ACTION_DATA/IMAGE/SIZE \
                    /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/MD5 \
                    /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/SHA1 \
                    /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/FORMAT \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BRIDGE_LIST \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/POOL_NAME \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_USER \
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_CONF)

unset i

SRC="${XPATH_ELEMENTS[i++]}"
SIZE="${XPATH_ELEMENTS[i++]}"
MD5="${XPATH_ELEMENTS[i++]}"
SHA1="${XPATH_ELEMENTS[i++]}"
FORMAT="${XPATH_ELEMENTS[i++]:-raw}"
BRIDGE_LIST="${XPATH_ELEMENTS[i++]}"
POOL_NAME="${XPATH_ELEMENTS[i++]:-$POOL_NAME}"
CEPH_USER="${XPATH_ELEMENTS[i++]}"
CEPH_CONF="${XPATH_ELEMENTS[i++]}"

DST_HOST=`get_destination_host $ID`

if [ -z "$DST_HOST" ]; then
    error_message "Datastore template missing 'BRIDGE_LIST' attribute."
    exit -1
fi

IMPORT_SOURCE="rbd://$DST_HOST/$SRC"

if [ -n "$CEPH_USER" ]; then
    RBD="$RBD --id ${CEPH_USER}"
    IMPORT_SOURCE="${IMPORT_SOURCE}?CEPH_USER=${CEPH_USER}"
fi

if [ -n "$CEPH_CONF" ]; then
    RBD="$RBD --conf ${CEPH_CONF}"

    if [ -n "$CEPH_USER" ]; then
        IMPORT_SOURCE="${IMPORT_SOURCE}&"
    else
        IMPORT_SOURCE="${IMPORT_SOURCE}?"
    fi

    IMPORT_SOURCE="${IMPORT_SOURCE}CEPH_CONF=${CEPH_CONF}"
fi

#-------------------------------------------------------------------------------
# Output image source and format
#-------------------------------------------------------------------------------

INFO_SCRIPT=$(cat <<EOF
if [ -z "$MD5" ]; then
    CHECKSUM=\$(
        $RBD export ${SRC} - | ${MD5SUM} | cut -f1 -d' '
        ps=\$PIPESTATUS

        if [ "\$ps" != "0" ]; then
            exit \$ps
        fi
    )

    status=\$?

    [ "\$status" != "0" ] && exit \$status
else
    CHECKSUM="$MD5"
fi

if [ -z "\$CHECKSUM" ]; then
    exit 1
fi

cat <<EOT
<MD5><![CDATA[\$CHECKSUM]]></MD5>
<SIZE><![CDATA[$SIZE]]></SIZE>
<FORMAT><![CDATA[${FORMAT}]]></FORMAT>
EOT
EOF
)

INFO=$(ssh_monitor_and_log "$DST_HOST" "$INFO_SCRIPT" "Image info script" 2>&1)
INFO_STATUS=$?

if [ "$INFO_STATUS" != "0" ]; then
    echo "$INFO"
    exit $INFO_STATUS
fi

cat <<EOF
<IMPORT_INFO>
    <IMPORT_SOURCE><![CDATA[$IMPORT_SOURCE]]></IMPORT_SOURCE>
    $INFO
    <DISPOSE>NO</DISPOSE>
</IMPORT_INFO>"
EOF

